package com.miskatmobile.bb.linkedin;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import net.rim.device.api.ui.component.Dialog;

public class RestClient {
	private Hashtable params;
	private int responseCode;
	private String response;
	public static final int GET = 1;
	public static final int POST = 2;
	public static final int DELETE = 3;
	private String url;
	private OAuthLibrary oauthLib;

	public RestClient() {
		params = new Hashtable();
		oauthLib = new OAuthLibrary();
	}

	public void setTokenWithSecret(String token, String secret){
		oauthLib.setTokenWithSecret(token, secret);
	}
	
	public void setUrl(String url) {
		params.clear();
		this.url = url;
	}

	public void addParam(String name, String value) {
		params.put(name, value);
	}

	public String getResponse(){
		return response;
	}
	
	public void execute(int method) {
		InputStream inStream = null;
		OutputStream outStream = null;
		HttpConnection httpConn = null;
		// Dialog.alert(header);
		
		switch (method) {
		case GET:

			String header = oauthLib.createHeader(url, HttpConnection.GET, params);
			try {

				/*
				 * HttpConnectionFactory factory = new HttpConnectionFactory(
				 * url, HttpConnectionFactory.TRANSPORT_WIFI |
				 * HttpConnectionFactory.TRANSPORT_WAP2 |
				 * HttpConnectionFactory.TRANSPORT_BIS |
				 * HttpConnectionFactory.TRANSPORT_BES |
				 * HttpConnectionFactory.TRANSPORT_DIRECT_TCP); httpConn =
				 * factory.getNextConnection();
				 */
				httpConn = (HttpConnection) Connector.open(url + "?" +header,
						Connector.READ_WRITE);
				httpConn.setRequestMethod(HttpConnection.GET);
				//httpConn.setRequestProperty("WWW-Authenticate", "OAuth realm="
					//	+ Const.ROOT_URL);
				httpConn.setRequestProperty("Content-Type",
						"application/x-www-form-urlencoded");
				httpConn.setRequestProperty("Content-Length",
						Integer.toString(header.getBytes().length));

				outStream = httpConn.openOutputStream();
				outStream.write(header.getBytes());
				// outStream.flush();
				outStream.close();
				outStream = null;

				inStream = httpConn.openInputStream();
				int resp = httpConn.getResponseCode();
				String content = "";
				if (resp == HttpConnection.HTTP_OK) {
					StringBuffer buffer = new StringBuffer();
					int ch;
					while ((ch = inStream.read()) != -1) {
						buffer.append((char) ch);
					}
					content = buffer.toString();
					response = content;
					System.out.println("content: " + content);
				} else {
					StringBuffer buffer = new StringBuffer();
					int ch;
					while ((ch = inStream.read()) != -1) {
						buffer.append((char) ch);
					}
					content = buffer.toString();
					response = content;
					//System.out.println("content: " + content);
					Dialog.alert(response);
				}

			} catch (IOException e) {
				response = "exception";
			} catch (Exception nc) {
				response = "noConnection";
			} finally {
				try {
					httpConn.close();
					inStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			break;
		case POST:
			header = oauthLib.createHeader(url, HttpConnection.POST, params);
			try {

				/*
				 * HttpConnectionFactory factory = new HttpConnectionFactory(
				 * url, HttpConnectionFactory.TRANSPORT_WIFI |
				 * HttpConnectionFactory.TRANSPORT_WAP2 |
				 * HttpConnectionFactory.TRANSPORT_BIS |
				 * HttpConnectionFactory.TRANSPORT_BES |
				 * HttpConnectionFactory.TRANSPORT_DIRECT_TCP); httpConn =
				 * factory.getNextConnection();
				 */

				Dialog.alert("header: " +header);
				httpConn = (HttpConnection) Connector.open(url,
						Connector.READ_WRITE);
				httpConn.setRequestMethod(HttpConnection.POST);
				//httpConn.setRequestProperty("WWW-Authenticate", "OAuth realm="
					//	+ Const.ROOT_URL);
				httpConn.setRequestProperty("Content-Type",
						"application/x-www-form-urlencoded");
				httpConn.setRequestProperty("Content-Length",
						Integer.toString(header.getBytes().length));

				outStream = httpConn.openOutputStream();
				outStream.write(header.getBytes());
				// outStream.flush();
				outStream.close();
				outStream = null;

				inStream = httpConn.openInputStream();
				int resp = httpConn.getResponseCode();
				String content = "";
				if (resp == HttpConnection.HTTP_OK) {
					StringBuffer buffer = new StringBuffer();
					int ch;
					while ((ch = inStream.read()) != -1) {
						buffer.append((char) ch);
					}
					content = buffer.toString();
					response = content;
					System.out.println("content: " + content);
					// String[] token = new String[2];
					// Const.token = token[0] =
					// content.substring(content.indexOf((Const.OAUTH_TOKEN+"="))+(Const.OAUTH_TOKEN+"=").length(),
					// content.indexOf('&'));
					// Const.tokenSecret = token[1] =
					// content.substring(content.indexOf((Const.OAUTH_TOKEN_SECRET+"="))+(Const.OAUTH_TOKEN_SECRET+"=").length(),
					// content.indexOf("&user"));

					// PreferencesStore ps = new PreferencesStore();
					// ps.setToken(token);
				} else {
					StringBuffer buffer = new StringBuffer();
					int ch;
					while ((ch = inStream.read()) != -1) {
						buffer.append((char) ch);
					}
					content = buffer.toString();
					response = content;
					System.out.println("content: " + content);
				}

			} catch (IOException e) {
				response = "exception";
			} catch (Exception nc) {
				response = "noConnection";
			} finally {
				try {
					httpConn.close();
					inStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			break;
		case DELETE:
			break;
		}
	}
}
